<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/7
 * Time: 10:21
 * 库存管理
 */
class stock extends MY_Controller {
	public function __construct() {
		parent::__construct();

		$this->load->model('stock_model');
		$this->load->model('stockPut_model');
		$this->load->model('stockOut_model');

		$this->load->model('goods_model');
//        $this->load->model('goodsImage_model');
		$this->load->model('goodsStockPrice_model');
		$this->load->model('purchaseOrder_model');
		$this->load->model('returnOrder_model');
		$this->load->model('supplier_model');
		$this->load->model('check_model');

		$this->load->model('project_model');

	}

	/**
	 * 商品列表
	 */
	public function index() {

		$page_num = $this->config->item('fenye')['pageNum']; //每页的数据
		$start_position = ''; //初始数据
		$page = ($this->input->get('page')); //获取页码数量
		if (!$page) {
			$num = 1;
			$start_position = ($num - 1) * $page_num;
		} else {
			$start_position = ($page - 1) * $page_num;
		}

		$goods = $this->goods_model->page('goods', 0, $page_num, $start_position); //limit查询的数据

		if ($goods[0]) {
			$ids = array_column($goods[0], 'id');
			foreach ($ids as $v) {
				$arg2 = [
					'product_id' => $v,
				];
				$goodsStock[] = $this->goodsStockPrice_model->getByFieldQueryData('id,product_id,specification_group_names,stock,product_stock_priceId', $arg2, 1); //limit查询的数据
			}

			$ckIds = array_column($goods[0], 'ckId');
			foreach ($ckIds as $k8 => $v8) {
				$arg3 = [
					'product_id' => $v8,
				];

				$goodsCkStock[] = $this->goodsStockPrice_model->getByFieldQueryData('product_id,stock,specification_group_names,product_stock_priceId', $arg3, 1); //limit查询的数据

			}

//            echo '<pre>';
			//            print_r($goodsCkStock);
			//            die();
			foreach ($goods[0] as $k5 => $v5) {
				foreach ($goodsCkStock as $k6 => $v6) {
					foreach ($v6 as $k7 => $v7) {

						if (isset($v7['specification_group_names']) || !empty($v7['specification_group_names'])) {
							if ($v7['product_id'] == $v5['ckId']) {
								$goods[0][$k5]['att'] = $v6;

								$goods[0][$k5]['stock'] = $v7['stock'];
								$goods[0][$k5]['product_stock_priceId'] = $v7['product_stock_priceId'];

							}
						} else {
							if ($v7['product_id'] == $v5['id']) {
								$goods[0][$k5]['att'] = $v6;

								$goods[0][$k5]['stock'] = $v7['stock'];
								$goods[0][$k5]['product_stock_priceId'] = $v7['product_stock_priceId'];
							}
						}
					}
				}
			}

			foreach ($goods[0] as $k3 => $v3) {
				foreach ($goodsStock as $k2 => $v2) {

					foreach ($v2 as $k4 => $v4) {
						if (isset($v4['specification_group_names']) || !empty($v4['specification_group_names'])) {
							if ($v4['product_id'] == $v3['id']) {
								$goods[0][$k3]['att'] = $v2;
							}

						} else {
							if ($v4['product_id'] == $v3['id']) {
								$goods[0][$k3]['att'] = $v2;
							}
						}
					}

				}
			}

			$viewData = [
				'list' => $goods[0], //这里得到从数据库中的当页数据
				'totalNum' => $goods['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
			];

			$this->load->view('/admin/stock/list', $viewData);
		} else {
			$viewData = [
				'list' => '', //这里得到从数据库中的当页数据
				'totalNum' => $goods['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
			];

			$this->load->view('/admin/stock/list', $viewData);
		}

		//id , product_no , name , one_category , image , file_path 并且is_main等于1, stock , specification_group_names

	}

	//修改库存接口
	public function edit() {
		$stock = [
			'stock' => $_POST['stock'],
		];

		foreach ($_POST['id'] as $k => $v) {
			if (strlen($v) == 32) {
				$where = [
					'product_stock_priceId' => $v,

				];
				__logInfo('product_stock_priceId', $v);
			} else {
				$where = [
					'id' => $v,
				];
			}

			$info = [
				'editUser' => $_COOKIE['nickname'],
				'product_stock_priceId' => isset($where['product_stock_priceId']) ? $where['product_stock_priceId'] : $where['id'],
			];

			__logInfo('库存修改记录', json_encode($info, JSON_UNESCAPED_UNICODE));

//            echo strlen('B5E1EBA47DA111E7A96B3497F625B277');

			$result = $this->goodsStockPrice_model->edit($stock, $where);

			__logInfo('库存修改接口', $result);

			//修改成功之后调用51ck库存修改接口
			if ($result) {
				$a = [
					'success' => 1,
				];

				$this->load->library('Curl');
				$prefix = $this->config->item('51ck')['prefix'];
				$ckStock = [
					'productStockPriceId' => $v,
					'stock' => $_POST['stock'],
				];
				$a = $this->curl->curlPost($prefix . 'productStockPrice/stockEdit', $ckStock);

				__logInfo('51ck库存修改数据包', json_encode($a));

				__logInfo('51ck的的地址', $prefix . 'productStockPrice/stockEdit');
			} else {
				$a = [
					'error' => $result,
				];
			}

			echo json_encode($a, JSON_UNESCAPED_UNICODE);

		}

	}

	//编辑页面
	private function editView() {

		//读取用户名根据get id
		$arg = [
			'id' => $this->input->get('id'),
		];

		$result['stockList'] = $this->stock_model->getByFieldQueryData('*', $arg, 0);

		$this->load->view('/admin/stock/edit', $result);
	}

	//商品入库
	public function put() {
//        echo 123123;
		$page_num = $this->config->item('fenye')['pageNum']; //每页的数据
		$start_position = ''; //初始数据
		$page = ($this->input->get('page')); //获取页码数量
		if (!$page) {
			$num = 1;
			$start_position = ($num - 1) * $page_num;
		} else {
			$start_position = ($page - 1) * $page_num;
		}

		//判断搜索内容是否存在存在就继续

		$data = $this->stockPut_model->page('stockPut', 0, $page_num, $start_position); //limit查询的数据

		foreach ($data[0] as $v) {
			if ($v['purchaseCode']) {
				$arg = [
					'purchaseCode' => $v['purchaseCode'],
				];
				$returnName[] = $this->purchaseOrder_model->getByFieldQueryData('id,purchaseCode,name,status', $arg, 0);
			}
		}

		foreach ($data[0] as $k2 => $v2) {
			if (isset($returnName) && $returnName) {
				foreach ($returnName as $v3) {
					if ($v2['purchaseCode']) {
						//判断出库单的code == purchaseCode,
						if ($v2['purchaseCode'] == $v3['purchaseCode']) {
							$data[0][$k2]['returnName'] = $v3['name'];
							$data[0][$k2]['returnId'] = $v3['id'];
							$data[0][$k2]['returnStatus'] = $v3['status'];
						}
					} else {
						$data[0][$k2]['returnName'] = '';
						$data[0][$k2]['returnId'] = '';
						$data[0][$k2]['returnStatus'] = '';

					}
				}
			} else {
				$data[0][$k2]['returnName'] = '';
				$data[0][$k2]['returnId'] = '';
				$data[0][$k2]['returnStatus'] = '';

			}
		}

		if ($data[0]) {
			$viewData = [
				'list' => $data[0], //这里得到从数据库中的当页数据
				'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
				'supplier' => $this->supplier_model->getData('id,name', 1),

			];
		} else {
			$viewData = [
				'list' => $data[0], //这里得到从数据库中的当页数据
				'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
				'supplier' => $this->supplier_model->getData('id,name', 1),

			];
		}

//        __format($viewData);
		$this->load->view('/admin/stock/put/list', $viewData);

	}

	//商品入库详情
	public function putDetail() {
		//编号和日期从stock_put读取数据
		$arg = [
			'id' => $this->input->get('id'),
		];

		$data['list'] = $this->stockPut_model->getByFieldQueryData('*', $arg, 0);

		//如果不存在直接跳转到404页面
		if (!$data['list']) {
			redirect('/home/error');
		}

//        echo '<pre>';
		//        print_r($data);
		//        die();

		//根据编号查询status
		$arg2 = [
			'purchaseCode' => $data['list']['purchaseCode'],
		];
		$purStatus = $this->purchaseOrder_model->getByFieldQueryData('status', $arg2, 0);
//        $purId = $this->purchaseOrder_model->getByFieldQueryData('id',$arg2,0);

//        echo '<pre>';
		//        print_r($purStatus);
		//        die();
		//        $data['list']['purId']  = $purId['id'];
		$data['list']['purStatus'] = $purStatus['status'];
		$data['list']['logisticsInfo'] = json_decode($data['list']['logisticsInfo'], true);
		$data['list']['payInfo'] = json_decode($data['list']['payInfo'], true);
		$data['list']['goodsMessage'] = json_decode($data['list']['goodsMessage'], true);

//        echo '<pre>';
		//        print_r($data);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k => $v) {
			if (isset($v['dataId'])) {
				$arg = [
					'product_id' => $v['dataId'],
				];
			} else {
				$data['list']['goodsMessage'][$k]['dataId'] = '';
			}

			$isStock = $this->goodsStockPrice_model->getByFieldQueryData($fields = '*', $arg, $isMore = 0);

		}

//        print_r($isStock);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k2 => $v2) {
			if (isset($isStock)) {
				if (!empty($v2['order'])) {
					$data['list']['goodsMessage'][$k2]['isStock'] = 1;

				} else {
					$data['list']['goodsMessage'][$k2]['isStock'] = 0;
				}
			} else {
				$data['list']['goodsMessage'][$k2]['isStock'] = 0;
			}
		}

//        __format($data);die;
		$this->load->view('/admin/stock/put/detail', $data);
	}

	//商品入库新增页面
	public function putAddView() {
		$data['project'] = $this->project_model->getData('id,name', 1);
		$data['supplier'] = $this->supplier_model->getData('id,name', 1);

		$this->load->view('/admin/stock/put/add', $data);
	}

	//商品入库新增
	public function putAdd() {
		//新增单据，状态为待审核。
		//        echo json_encode($_POST,JSON_UNESCAPED_UNICODE);
		//        die;

		//因为这里是直接添加的,如果直接到审核中.

		if ($_POST['action'] == '提交') {
			//待审核状态，表示已提交
			$status = 3;
		} else {
			//待提交状态，还未提交能编辑
			$status = 2;
		}

		$stockOut = [
			'type' => '商品入库单',
			'stockPutCode' => $_POST['stockPutCode'],
			'createMan' => $_POST['createMan'],
			'content' => $this->input->post('content'),
			'createTime' => strtotime($_POST['time']),
			'supplier' => $this->input->post('supplier'), //收货方
			'status' => $status,
			'attribute' => $this->input->post('attribute'),
			'goodsMessage' => $_POST['goodsMessage'],

			'director' => $_POST['director'], //主管审批
			'president' => $_POST['president'], //副总审批

			'name' => $_POST['name'], //单据名称
			'source' => $_POST['source'], //所属项目
		];

		//数据并新增到审核管理中。
		$stockOutId = $this->stockPut_model->addId($stockOut);

		//数据添加成功,并且状态等于三的时候,添加到审核管理中
		if ($stockOutId && $status == 3) {
			$checkData = [
				'type' => 1,
				'relevanceId' => $stockOutId,
				'checkinfo' => '',
				'checker' => trim($this->input->post('director')),
				'status' => 0,
				'checkStatus' => 0,
				'howLevelCheck' => 2,
				'createTime' => time(),
				'checkOrderType' => '商品入库单',
			];

			$this->check_model->add($checkData);
		}

	}

	//商品入库编辑页面
	public function putEditView() {
//编号和日期从stock_put读取数据
		$arg = [
			'id' => $this->input->get('id'),
		];

		$data['list'] = $this->stockPut_model->getByFieldQueryData('*', $arg, 0);

		//如果不存在直接跳转到404页面
		if (!$data['list']) {
			redirect('/home/error');
		}

		$data['list']['goodsMessage'] = json_decode($data['list']['goodsMessage'], true);

		foreach ($data['list']['goodsMessage'] as $k => $v) {
			if (isset($v['dataId'])) {
				$arg = [
					'product_id' => $v['dataId'],
				];
			} else {
				$data['list']['goodsMessage'][$k]['dataId'] = '';
			}

			$isStock = $this->goodsStockPrice_model->getByFieldQueryData($fields = '*', $arg, $isMore = 0);

		}

		foreach ($data['list']['goodsMessage'] as $k2 => $v2) {
			if (isset($isStock)) {
				if (!empty($v2['order'])) {
					$data['list']['goodsMessage'][$k2]['isStock'] = 1;

				} else {
					$data['list']['goodsMessage'][$k2]['isStock'] = 0;
				}
			} else {
				$data['list']['goodsMessage'][$k2]['isStock'] = 0;
			}
		}

		$data['project'] = $this->project_model->getData('id,name', 1);

		$this->load->view('/admin/stock/put/edit', $data);

	}

	//商品入库编辑页面
	public function putEdit() {
		//新增单据，状态为待审核。

		//因为这里是直接添加的,如果直接到审核中.

		if ($_POST['action'] == '提交') {
			//待审核状态，表示已提交
			$status = 3;
		} else {
			//待提交状态，还未提交能编辑
			$status = 2;
		}

		$stockOut = [
			'type' => '商品入库单',
			'stockPutCode' => $_POST['stockPutCode'],
			'createMan' => $_POST['createMan'],
			'content' => $this->input->post('content'),
			'createTime' => strtotime($_POST['time']),
			'supplier' => $this->input->post('supplier'), //收货方
			'status' => $status,
			'attribute' => $this->input->post('attribute'),
			'goodsMessage' => $_POST['goodsMessage'],

			'director' => $_POST['director'], //主管审批
			'president' => $_POST['president'], //副总审批

			'name' => $_POST['name'], //单据名称
			'source' => $_POST['source'], //所属项目
		];

		$where = [
			'id' => $_POST['id'],
		];

		//数据并新增到审核管理中。
		$stockOutId = $this->stockPut_model->edit($stockOut, $where);
		//数据添加成功,并且状态等于三的时候,添加到审核管理中
		if ($stockOutId && $status == 3) {
			$checkData = [
				'type' => 1,
				'relevanceId' => $_POST['id'],
				'checkinfo' => '',
				'checker' => trim($this->input->post('director')),
				'status' => 0,
				'checkStatus' => 0,
				'howLevelCheck' => 2,
				'createTime' => time(),
				'checkOrderType' => '商品入库单',
			];

			$this->check_model->add($checkData);
		}

	}

	//商品入库单删除
	public function putRemove() {

		$id = $this->input->get('id');

		$where = [
			'id' => $id,
		];

		$this->stockPut_model->remove($where);

		//为空是项目单,为假的需求单
		if (isset($_GET['type'])) {
			redirect('/admin/stock/put?page=1');
		} else {
			redirect('/admin/stock/put?page=1');
		}
	}

	//商品出库
	public function out() {
//        echo 123123;
		$page_num = $this->config->item('fenye')['pageNum']; //每页的数据
		$start_position = ''; //初始数据
		$page = ($this->input->get('page')); //获取页码数量
		if (!$page) {
			$num = 1;
			$start_position = ($num - 1) * $page_num;
		} else {
			$start_position = ($page - 1) * $page_num;
		}

		$data = $this->stockOut_model->page('stockOut', 0, $page_num, $start_position); //limit查询的数据

		foreach ($data[0] as $v) {
			if ($v['returnCode']) {
				$arg = [
					'code' => $v['returnCode'],
				];
				$returnName[] = $this->returnOrder_model->getByFieldQueryData('id,code,name,status', $arg, 0);
			}
		}

		foreach ($data[0] as $k2 => $v2) {
			if (isset($returnName) && $returnName) {
				foreach ($returnName as $v3) {
					if ($v2['returnCode']) {
						//判断出库单的code == 关联的code,
						if ($v2['returnCode'] == $v3['code']) {
							$data[0][$k2]['returnName'] = $v3['name'];
							$data[0][$k2]['returnId'] = $v3['id'];
							$data[0][$k2]['returnStatus'] = $v3['status'];
						}
					} else {
						$data[0][$k2]['returnName'] = '';
						$data[0][$k2]['returnId'] = '';
						$data[0][$k2]['returnStatus'] = '';
					}
				}
			} else {
				$data[0][$k2]['returnName'] = '';
				$data[0][$k2]['returnId'] = '';
				$data[0][$k2]['returnStatus'] = '';
			}
		}

//        __format($data);

		if ($data[0]) {
			$viewData = [
				'list' => $data[0], //这里得到从数据库中的当页数据
				'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
				'supplier' => $this->supplier_model->getData('id,name', 1),

			];
		} else {
			$viewData = [
				'list' => $data[0], //这里得到从数据库中的当页数据
				'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
				'pageNum' => $page_num, //每页显示多少条
				'supplier' => $this->supplier_model->getData('id,name', 1),

			];
		}
		$this->load->view('/admin/stock/out/list', $viewData);

	}

	//商品出库详情
	public function outDetail() {
		//编号和日期从stock_put读取数据
		$arg = [
			'id' => $this->input->get('id'),
		];

		$data['list'] = $this->stockOut_model->getByFieldQueryData('*', $arg, 0);

		//如果不存在直接跳转到404页面
		if (!$data['list']) {
			redirect('/home/error');
		}

		//根据编号查询status
		$arg2 = [
			'code' => $data['list']['returnCode'],
		];
		$purStatus = $this->returnOrder_model->getByFieldQueryData('id', $arg2, 0);
//
		//        print_r($purStatus);
		//        die();
		$data['list']['retId'] = $purStatus['id'];

		$data['list']['goodsMessage'] = json_decode($data['list']['goodsMessage'], true);

//        echo '<pre>';
		//        print_r($data);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k => $v) {
			if (isset($v['dataId'])) {
				$arg = [
					'product_id' => $v['dataId'],
				];
			} else {
				$data['list']['goodsMessage'][$k]['dataId'] = '';
			}

			$isStock = $this->goodsStockPrice_model->getByFieldQueryData($fields = '*', $arg, $isMore = 0);

		}

//        print_r($isStock);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k2 => $v2) {
			if (isset($isStock)) {
				if (!empty($v2['order'])) {
					$data['list']['goodsMessage'][$k2]['isStock'] = 1;

				} else {
					$data['list']['goodsMessage'][$k2]['isStock'] = 0;
				}
			} else {
				$data['list']['goodsMessage'][$k2]['isStock'] = 0;
			}
		}

		$this->load->view('/admin/stock/out/detail', $data);
	}

	//商品出库新增页面
	public function outAddView() {

		$data['project'] = $this->project_model->getData('id,name', 1);
//        $data['supplier'] = $this->supplier_model->getData('id,name', 1);

		$this->load->view('/admin/stock/out/add', $data);
	}

	//商品出库新增
	public function outAdd() {
		//新增单据，状态为待审核。
		//        echo json_encode($_POST,JSON_UNESCAPED_UNICODE);
		//        die;

		//因为这里是直接添加的,如果直接到审核中.

		if ($_POST['action'] == '提交') {
			//待审核状态，表示已提交
			$status = 3;
		} else {
			//待提交状态，还未提交能编辑
			$status = 2;
		}

		$stockOut = [
			'type' => '商品出库单',
			'stockOutCode' => $_POST['stockOutCode'],
			'createMan' => $_POST['createMan'],
			'content' => $this->input->post('content'),
			'createTime' => strtotime($_POST['time']),
			'supplier' => $this->input->post('supplier'), //收货方
			'status' => $status,
			'attribute' => $this->input->post('attribute'),
			'goodsMessage' => $_POST['goodsMessage'],

			'director' => $_POST['director'], //主管审批
			'president' => $_POST['president'], //副总审批

			'name' => $_POST['name'], //单据名称
			'source' => $_POST['source'], //所属项目
		];

		//数据并新增到审核管理中。
		$stockOutId = $this->stockOut_model->addId($stockOut);
		//数据添加成功,并且状态等于三的时候,添加到审核管理中
		if ($stockOutId && $status == 3) {
			$checkData = [
				'type' => 1,
				'relevanceId' => $stockOutId,
				'checkinfo' => '',
				'checker' => trim($this->input->post('director')),
				'status' => 0,
				'checkStatus' => 0,
				'howLevelCheck' => 2,
				'createTime' => time(),
				'checkOrderType' => '商品出库单',
			];

			$this->check_model->add($checkData);
		}

	}

	//商品出库编辑页面
	public function outEditView() {
//编号和日期从stock_put读取数据
		$arg = [
			'id' => $this->input->get('id'),
		];

		$data['list'] = $this->stockOut_model->getByFieldQueryData('*', $arg, 0);

		//如果不存在直接跳转到404页面
		if (!$data['list']) {
			redirect('/home/error');
		}

		//根据编号查询status
		//        $arg2 = [
		//            'code' => $data['list']['returnCode']
		//        ];
		//        $purStatus = $this->returnOrder_model->getByFieldQueryData('id',$arg2,0);
		//
		//        print_r($purStatus);
		//        die();
		//        $data['list']['retId'] = $purStatus['id'];

		$data['list']['goodsMessage'] = json_decode($data['list']['goodsMessage'], true);

//        echo '<pre>';
		//        print_r($data);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k => $v) {
			if (isset($v['dataId'])) {
				$arg = [
					'product_id' => $v['dataId'],
				];
			} else {
				$data['list']['goodsMessage'][$k]['dataId'] = '';
			}

			$isStock = $this->goodsStockPrice_model->getByFieldQueryData($fields = '*', $arg, $isMore = 0);

		}

//        print_r($isStock);
		//        die();

		foreach ($data['list']['goodsMessage'] as $k2 => $v2) {
			if (isset($isStock)) {
				if (!empty($v2['order'])) {
					$data['list']['goodsMessage'][$k2]['isStock'] = 1;

				} else {
					$data['list']['goodsMessage'][$k2]['isStock'] = 0;
				}
			} else {
				$data['list']['goodsMessage'][$k2]['isStock'] = 0;
			}
		}

		$data['project'] = $this->project_model->getData('id,name', 1);

		$this->load->view('/admin/stock/out/edit', $data);

	}

	//商品出库编辑页面
	public function outEdit() {
		//新增单据，状态为待审核。
		//        echo json_encode($_POST,JSON_UNESCAPED_UNICODE);
		//        die;

		//因为这里是直接添加的,如果直接到审核中.

		if ($_POST['action'] == '提交') {
			//待审核状态，表示已提交
			$status = 3;
		} else {
			//待提交状态，还未提交能编辑
			$status = 2;
		}

		$stockOut = [
			'type' => '商品出库单',
			'stockOutCode' => $_POST['stockOutCode'],
			'createMan' => $_POST['createMan'],
			'content' => $this->input->post('content'),
			'createTime' => strtotime($_POST['time']),
			'supplier' => $this->input->post('supplier'), //收货方
			'status' => $status,
			'attribute' => $this->input->post('attribute'),
			'goodsMessage' => $_POST['goodsMessage'],

			'director' => $_POST['director'], //主管审批
			'president' => $_POST['president'], //副总审批

			'name' => $_POST['name'], //单据名称
			'source' => $_POST['source'], //所属项目
		];

		$where = [
			'id' => $_POST['id'],
		];

		//数据并新增到审核管理中。
		$stockOutId = $this->stockOut_model->edit($stockOut, $where);
		//数据添加成功,并且状态等于三的时候,添加到审核管理中
		if ($stockOutId && $status == 3) {
			$checkData = [
				'type' => 1,
				'relevanceId' => $_POST['id'],
				'checkinfo' => '',
				'checker' => trim($this->input->post('director')),
				'status' => 0,
				'checkStatus' => 0,
				'howLevelCheck' => 2,
				'createTime' => time(),
				'checkOrderType' => '商品出库单',
			];

			$this->check_model->add($checkData);
		}

	}

	public function outRemove() {

		$id = $this->input->get('id');

		$where = [
			'id' => $id,
		];

		$this->stockOut_model->remove($where);

		//为空是项目单,为假的需求单
		if (isset($_GET['type'])) {
			redirect('/admin/stock/out?page=1');
		} else {
			redirect('/admin/stock/out?page=1');
		}
	}

	//确定收货通过参数确定是入库还是出库
	public function confirm() {

		$where = [
			'id' => $_POST['id'],

		];

		__logInfo('库存管理出库如类型', $_POST['type']);

		if ($_POST['type'] == 'put') {
			//入库
			$data = [
				'status' => 1,
				'courierNumber' => $_POST['courierNumber'],
				'express' => $_POST['express'],
			];

			$putEdit = $this->stockPut_model->edit($data, $where);

			//出库成功之后，修改库存表的库存数量
			//通过id查询，goodsMessage信息
			if ($putEdit) {
				$goodsMessage = $this->stockPut_model->getByFieldQueryData('id,goodsMessage', $where, 0);

				$formatGoodsMessage = json_decode($goodsMessage['goodsMessage'], true);

				if ($formatGoodsMessage) {
					foreach ($formatGoodsMessage as $k => $v) {
						if ($v['stockPriceId']) {
							$v['quantity'] = $v['quantity'] ? $v['quantity'] : 0;
							$where3 = [
								'id' => $v['stockPriceId'],
							];

							$stock = [
								'stock',
								'stock +' . $v['quantity'],
								FALSE,
							];

							$this->goodsStockPrice_model->setFieldChange($where3, $stock);

						}
					}
				}

				redirect('/admin/stock/put?page=1');

			} else {
				echo __alert('入库失败！');

			}

		} else {
			//出库有关联的单号
			$data = [
				'status' => 1,
				'courierNumber' => $_POST['courierNumber'],
				'express' => $_POST['express'],
			];

			$edit = $this->stockOut_model->edit($data, $where);

			//出库成功之后，修改退换货表的状态为已完成发货
			if ($edit) {
				$data2 = [
					'status' => 4,
				];
				$where2 = [
					'code' => $_POST['returnCode'],
				];

				$this->returnOrder_model->edit($data2, $where2);

				//出库成功之后，修改库存表的库存数量
				//通过id查询，goodsMessage信息
				$goodsMessage = $this->stockOut_model->getByFieldQueryData('id,goodsMessage', $where, 0);

				$formatGoodsMessage = json_decode($goodsMessage['goodsMessage'], true);

				if ($formatGoodsMessage) {
					foreach ($formatGoodsMessage as $k => $v) {
						if ($v['stockPriceId']) {
							$v['quantity'] = $v['quantity'] ? $v['quantity'] : 0;
							$where3 = [
								'id' => $v['stockPriceId'],
							];

							$stock = [
								'stock',
								'stock -' . $v['quantity'],
								FALSE,
							];

							$this->goodsStockPrice_model->setFieldChange($where3, $stock);
						}
					}
				}
				redirect('/admin/stock/out?page=1');

			} else {

				echo __alert('出库失败！');
			}
		}
	}

	//退货单作废功能
	public function invalid() {
		$where = [
			'id' => $this->input->get('id'),
		];

		if ($_GET['type'] == 'put') {
			$data = [
				'status' => 2, //作废功能
			];

			$this->stockPut_model->edit($data, $where);
			redirect('/admin/stock/put/?page=1');

		} else {
			$data = [
				'status' => 2, //作废功能
			];

			$this->stockOut_model->edit($data, $where);
			redirect('/admin/stock/out/?page=1');
		}

	}

	//todo 导出功能
	public function daochu() {

	}

}